package ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTable;
import javax.swing.JTextField;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import protocal.DataPackage;
import protocal.DataPackage.MessageType;
import client.RegisterClient;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class RegisterFrame extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
	private final static Logger logger = LogManager.getLogger(LoginFrame.class);
	
	private JTextField nameTf;
	private JPasswordField psdPf;
	private JPasswordField confirmpsdPf;
	private JButton registerlBtn;
	private JButton loginBtn;
	private LoginFrame loginFrame;
	
	public RegisterFrame(){
		this.setSize(400,320);
		this.setResizable(false);
		init();
		addEvent();
		this.setVisible(false);
	}
	
	public void init(){
		this.setLayout(null);
		this.setLocationRelativeTo(null);
		nameTf = new JTextField();
		psdPf = new JPasswordField();
		confirmpsdPf = new JPasswordField();
		registerlBtn = new JButton("register");
		loginBtn = new JButton("login");
		JLabel nameLable = new JLabel("username:",nameTf.LEFT);
		JLabel psdfLable = new JLabel("password:",psdPf.LEFT);
		JLabel confirmpsdPfLable = new JLabel("confirm:",confirmpsdPf.LEFT);
		nameLable.setBounds(10, 20, 80, 40);
		nameTf.setBounds(80,20,250,30);
		psdfLable.setBounds(10, 80, 80, 40);
		psdPf.setBounds(80, 80, 250, 30);
		confirmpsdPfLable.setBounds(10, 140, 80, 40);
		confirmpsdPf.setBounds(80, 140, 250, 30);
		registerlBtn.setBounds(60, 200, 80, 40);
		loginBtn.setBounds(200,200,80,40);
		this.add(nameLable);
		this.add(psdfLable);
		this.add(confirmpsdPfLable);
		this.add(nameTf);
		this.add(psdPf);
		this.add(confirmpsdPf);
		this.add(registerlBtn);
		this.add(loginBtn);
	}
	
	public void setLoginFrame(LoginFrame loginFrame){
		this.loginFrame = loginFrame;
	}
	
	public void addEvent(){
		registerlBtn.addActionListener(this);
		loginBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				loginFrame.setVisible(true);
				setVisible(false);
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String name = nameTf.getText();
		String psd = psdPf.getText();
		String confirmpsd = confirmpsdPf.getText(); 
		if (name==null||name.equals("")) {
			JOptionPane.showMessageDialog(null, "please enter your name", "alert", JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (psd==null||psd.equals("")) {
			JOptionPane.showMessageDialog(null, "please enter password", "alert", JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (confirmpsd==null||confirmpsd.equals("")) {
			JOptionPane.showMessageDialog(null, "please enter confirm password", "alert", JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (!psd.equals(confirmpsd)) {
			JOptionPane.showMessageDialog(null, "Entered passwords differ", "alert", JOptionPane.ERROR_MESSAGE);
			return;
		}
		DataPackage dataPackage = new DataPackage();
		dataPackage.setFromeName(nameTf.getText());
		dataPackage.setToName("\\");
		dataPackage.setMessageType(MessageType.REGISTER);
		dataPackage.setMessageData(psdPf.getText());
		dataPackage.setTime(System.currentTimeMillis());
		RegisterClient client = new RegisterClient();
		BufferedWriter writer = client.getWriter();
		BufferedReader reader = client.getReader();
		ObjectMapper mapper = new ObjectMapper();
		try {
			writer.write(mapper.writeValueAsString(dataPackage)+"\n");
			writer.flush();
			String data = reader.readLine();
			DataPackage res = mapper.readValue(data, DataPackage.class);
			if (res.getMessageData().equals("success")) {
				JOptionPane.showMessageDialog(null, "register success", "alert", JOptionPane.OK_OPTION);
				nameTf.setText("");
				psdPf.setText("");
				confirmpsdPf.setText("");
			}else{
				JOptionPane.showMessageDialog(null, res.getMessageData(), "alert", JOptionPane.ERROR_MESSAGE);
			}
		} catch (JsonProcessingException e1) {
			logger.error(e1);
		} catch (IOException e1) {
			logger.error(e1);
		}
		loginFrame.setVisible(true);
		setVisible(false);
		
	}
	
	public static void main(String[] args) {
		new RegisterFrame();
	}
}
